# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=meilisearch
pkgver=1.1.1
pkgrel=4
pkgdesc="A lightning-fast search engine that fits into your apps, websites and workflow"
url="https://www.meilisearch.com/"
# ppc64le, riscv64, s390x, loongarch64: fails to build ring crate
# armhf, armv7, x86: unsupported by upstream
arch="all !armhf !armv7 !ppc64le !riscv64 !s390x !x86 !loongarch64"
license="MIT"
depends="ca-certificates"
makedepends="
	cargo
	cargo-auditable
	mimalloc2-dev
	"
pkgusers="meilisearch"
pkggroups="meilisearch"
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="$pkgname-openrc"
_actixtls_ver=tls-v3.0.3
_deserr_rev=4657d42d2c6efb839345718182ab193730db5817
source="https://github.com/meilisearch/meilisearch/archive/v$pkgver/meilisearch-$pkgver.tar.gz
	https://github.com/actix/actix-net/archive/$_actixtls_ver/actix-net-$_actixtls_ver.tar.gz
	https://github.com/meilisearch/deserr/archive/$_deserr_rev/deserr-$_deserr_rev.tar.gz
	reformat-config.patch
	unbundle-ca-certs.patch
	tokenizer-flags.patch
	deserr-features.patch
	syslog.patch
	config.patch
	cargo-update-libc-lfs64.patch
	$pkgname.initd
	$pkgname.confd
	"
options="!check"  # FIXME: tests run out of file descriptors

# Disable analytics (telemetry / data collection), mini-dashboard (JS project)
# and specialized tokenizers (they are huge).
_cargo_opts="--frozen --no-default-features"

export CARGO_PROFILE_RELEASE_OPT_LEVEL=2

prepare() {
	cargo fetch --target="$CTARGET" --locked

	mkdir -p vendor
	mv "$srcdir"/actix-net-$_actixtls_ver vendor/actix-net
	mv "$srcdir"/deserr-$_deserr_rev vendor/deserr

	default_prepare

	# Build with patched actix-tls and deserr.
	# NOTE: Web patch actix-tls to get rid of f*cking bundled CA certificates
	#   (https://github.com/actix/actix-net/issues/462).
	# TODO: Remove deserr after https://github.com/meilisearch/deserr/pull/35 is released.
	cat >> Cargo.toml <<-EOF

		[patch.crates-io]
		actix-tls = { path = "./vendor/actix-net/actix-tls" }
		deserr = { path = "./vendor/deserr" }
	EOF

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided mimalloc.
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		mimalloc = { rustc-link-lib = ["mimalloc"] }
	EOF

	cargo fetch --target="$CTARGET"  # update after patching
}

build() {
	cargo auditable build $_cargo_opts --release
}

check() {
	cargo test $_cargo_opts --workspace
}

package() {
	install -D -m755 target/release/$pkgname -t "$pkgdir"/usr/bin/
	install -D -m640 -g "$pkggroups" config.toml "$pkgdir"/etc/meilisearch/config.toml

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	install -d -m755 -o "$pkgusers" -g "$pkggroups" "$pkgdir"/var/lib/$pkgname
}

sha512sums="
22508f10b782c72609d114b5629bc245331c4d5b03306286f25a96ce27c3ce5038b0f1821184bbd3de926ec5884dcaf7d156d3b3206e8cc2db719cf59ae419fa  meilisearch-1.1.1.tar.gz
52bb0556cf9e7462d267ec9878d2222d14bbee7176b80acc68d9b35005efa07375d4428b7cad2814d28895f43e6c35b5057ef2276bd3e252a04eed46e7e47a59  actix-net-tls-v3.0.3.tar.gz
332f36c2f3a2615cefd5fcf211e96b4da83921881b7faf761bbf1552170b76870fc70d780534f258e4eb4961f0bd95d47832c33a203d457e98941fac1c58eeb1  deserr-4657d42d2c6efb839345718182ab193730db5817.tar.gz
9a4966501267688b9e98e8b555d486e044386f781f5133cc1ec7576bc1dbefcf59ffaa675be1b9a6a9787cd723c8f903e634da86aa44ff2fb94925653bb99037  reformat-config.patch
63e29d8bb477431d1f5cf9b9589ca77694edba97f695e01beb5dc2041544cee72dc13ced786445173409c06d44488163aceb230233795140f93081420b00595b  unbundle-ca-certs.patch
62f74c28a2e755bf2f9e65653b71281e5065ff7276993ed98e2457eca4315a2729181b8238af1ec74911b98c80a4a2955d19223d507215d14f98ebf05f4c44e9  tokenizer-flags.patch
1e934f6ac31352b1c57a90e4c50fc26b1483134f2edaabd8c981f118c9c2a7729213aab6704f8f61e371429a8c59d80df5309075f7be66fbe746ce38a4baec2e  deserr-features.patch
bacc82d2d4654930cff3173fb09ece051c45adcb288458fce4be1930c80b59cdac5c4efaa81c2997be0f3833a96811a2485a0f1ef318eabffaa4b6b3ae964eac  syslog.patch
41b7a6c0fa049216437e00d8f1cbea336e30d527d8cc626b9bd1885f5d817a67f30af324efc2a498611bd3b781e926de6150dfeb89029433d6163191c671681b  config.patch
f8eb5382934a40ff6677c6b8b015e8cb3689ea65e2505c73ab15402d6ec313fd92617975ffe4569e65d4e9b07c0436a3162b33bb481d4c6e27daf83880f57362  cargo-update-libc-lfs64.patch
cc2ca5cc2d7baa3d17bb2d0798211599264d11017e2f8139bec07e719b6bf99de2bb0e2eb64ba99f74ed0c299c30f12a7867c76abfe57279a714a14c030e8543  meilisearch.initd
601a8cd3509a6fc24a6445fd09acca00f082e7767db1d36bfa496ab06c91414a43881e3cdcd2176620c9844776e4a2f1954051a4ec34cc13bba6d5316a0c9e96  meilisearch.confd
"
